﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.ConnectionUI;

namespace QuanLyChungCu_V.BussinessTỉer
{
    class SQLConnection
    {
        private string _ConnectionString;
        public string ConnectionString { get {return _ConnectionString;}}
        public SQLConnection(string cfgPath)
        {
            DataConnectionDialog dlgDtc = new DataConnectionDialog();
            DataConnectionConfiguration dcs = new DataConnectionConfiguration(cfgPath);
            dcs.SaveSelectedSource(DataSource.SqlDataSource.Name);
            dcs.SaveSelectedProvider(DataProvider.SqlDataProvider.Name);
            dcs.LoadConfiguration(dlgDtc);
            //dlgDtc.DataSources.Add(DataSource.SqlDataSource);
            //dlgDtc.UnspecifiedDataSource.Providers.Add(DataProvider.SqlDataProvider);
            //dlgDtc.SelectedDataSource = DataSource.SqlDataSource;
            //dlgDtc.SelectedDataProvider = DataProvider.SqlDataProvider;
            if(dcs.GetSelectedConnectionString() == null)
                if (DataConnectionDialog.Show(dlgDtc) == System.Windows.Forms.DialogResult.OK)
                {
                    //MessageBox.Show(dlgDtc.ConnectionString);
                    //dcs.SaveSelectedConnectionString(dlgDtc.ConnectionString);
                    dlgDtc.SaveSelection = true;
                    dcs.SaveConfiguration(dlgDtc);
                }
            if (dcs.GetSelectedConnectionString() != null) _ConnectionString = dcs.GetSelectedConnectionString();
            else throw new ApplicationException("Connection string is invalid");
        }
    }
}
